#ifndef __locker_h__
#define __locker_h__

#include <windows.h>

class Locker
{
public:
    Locker()
    {
        InitializeCriticalSection( &m_criticalsection );
    }

    ~Locker()
    { 
        DeleteCriticalSection( &m_criticalsection );
    }

    void Lock()
    { 
        EnterCriticalSection( &m_criticalsection );
    }

    void Unlock()
    { 
        LeaveCriticalSection( &m_criticalsection );
    }

    BOOL TryLock()
    {
        return TryEnterCriticalSection( &m_criticalsection );
    }

private:
    CRITICAL_SECTION m_criticalsection;
};

class AutoLocker
{
public:
    AutoLocker( Locker &locker )
        : m_locker( locker )
    {
        m_locker.Lock();
    }

    ~AutoLocker()
    {
        m_locker.Unlock();
    }

protected:
    Locker &m_locker;
};

#define AutoLocker_(x)  AutoLocker __tmp_##x(x)

#endif